package swagenda.ui.components;

import java.util.ArrayList;
import java.util.Collections;

import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.apache.log4j.Logger;

import swagenda.data.Category;
import swagenda.ui.MainFrame;

public class SWAList extends JList
{
	private static final long serialVersionUID = 20080304L;
	Logger logger = Logger.getLogger(this.getClass());
	
	public ArrayList<Category> l;

	public SWAList(ArrayList<Category> l)
	{
		super();
		Collections.sort(l);
		setListData(l.toArray());
		this.l = l;
		setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

		getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				if (!e.getValueIsAdjusting()) {
		            MainFrame.mainFrame.reload();
		        }
			}
		});

//		setLayoutOrientation(JList.VERTICAL);
	}

	public ArrayList<Category> selection()
	{
		ArrayList<Category> selected = new ArrayList<Category>();
		ListSelectionModel lsm = getSelectionModel();
		int minIndex = lsm.getMinSelectionIndex();
		int maxIndex = lsm.getMaxSelectionIndex();
		for (int i = minIndex; i <= maxIndex; i++) {
			if (lsm.isSelectedIndex(i)) {
				selected.add(l.get(i));
			}
		}
		return selected;
	}
}
